<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-input v-model="queryParams.level" placeholder="请输入级别" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
          <el-option v-for="dict in typeDictDatas" :key="parseInt(dict.value)" :label="dict.label"
                     :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="责任人" prop="responsible">
        <el-input v-model="queryParams.responsible" placeholder="请输入责任人" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label"
                     :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['biz:emergency-reserve-plan:create']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['biz:emergency-reserve-plan:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="级别" align="center" prop="level"/>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.BIZ_EMERGENCY_RESERVE_PLAN_TYPE" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="responsible"/>
      <el-table-column :show-overflow-tooltip="true" label="备注" align="center" prop="remark"/>
      <el-table-column label="开始年份" align="center" prop="startYear"/>
      <el-table-column label="结束年份" align="center" prop="endYear"/>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="已完成次数" align="center" prop="numberOfFinish"/>
      <el-table-column label="附件" align="center" prop="appendix" style="width: auto">
        <template slot-scope="scope">
          <el-image style="width: auto; height: auto;"
                    :src="scope.row.appendix">
            <div slot="error"></div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-share"
                     @click="clickDetail(scope.row)">预案详情
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['biz:emergency-reserve-plan:update']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['biz:emergency-reserve-plan:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="dict in typeDictDatas"
              :key="parseInt(dict.value)"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="责任人" prop="responsible">
          <el-input v-model="form.responsible" placeholder="请输入责任人"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="开始年份" prop="startYear">
          <el-input v-model="form.startYear" placeholder="请输入开始年份"/>
        </el-form-item>
        <el-form-item label="结束年份" prop="endYear">
          <el-input v-model="form.endYear" placeholder="请输入结束年份"/>
        </el-form-item>
        <el-form-item label="一月份应演练次数" prop="januaryNumberOfDrills">
          <el-input v-model="form.januaryNumberOfDrills" placeholder="请输入一月份应演练次数"/>
        </el-form-item>
        <el-form-item label="二月份应演练次数" prop="februaryNumberOfDrills">
          <el-input v-model="form.februaryNumberOfDrills" placeholder="请输入二月份应演练次数"/>
        </el-form-item>
        <el-form-item label="三月份应演练次数" prop="marchNumberOfDrills">
          <el-input v-model="form.marchNumberOfDrills" placeholder="请输入三月份应演练次数"/>
        </el-form-item>
        <el-form-item label="四月份应演练次数" prop="aprilNumberOfDrills">
          <el-input v-model="form.aprilNumberOfDrills" placeholder="请输入四月份应演练次数"/>
        </el-form-item>
        <el-form-item label="五月份应演练次数" prop="mayNumberOfDrills">
          <el-input v-model="form.mayNumberOfDrills" placeholder="请输入五月份应演练次数"/>
        </el-form-item>
        <el-form-item label="六月份应演练次数" prop="juneNumberOfDrills">
          <el-input v-model="form.juneNumberOfDrills" placeholder="请输入六月份应演练次数"/>
        </el-form-item>
        <el-form-item label="七月份应演练次数" prop="julyNumberOfDrills">
          <el-input v-model="form.julyNumberOfDrills" placeholder="请输入七月份应演练次数"/>
        </el-form-item>
        <el-form-item label="八月份应演练次数" prop="augustNumberOfDrills">
          <el-input v-model="form.augustNumberOfDrills" placeholder="请输入八月份应演练次数"/>
        </el-form-item>
        <el-form-item label="九月份应演练次数" prop="septemberNumberOfDrills">
          <el-input v-model="form.septemberNumberOfDrills" placeholder="请输入九月份应演练次数"/>
        </el-form-item>
        <el-form-item label="十月份应演练次数" prop="octoberNumberOfDrills">
          <el-input v-model="form.octoberNumberOfDrills" placeholder="请输入十月份应演练次数"/>
        </el-form-item>
        <el-form-item label="十一月份应演练次数" prop="novemberNumberOfDrills">
          <el-input v-model="form.novemberNumberOfDrills" placeholder="请输入十一月份应演练次数"/>
        </el-form-item>
        <el-form-item label="十二月份应演练次数" prop="decemberNumberOfDrills">
          <el-input v-model="form.decemberNumberOfDrills" placeholder="请输入十二月份应演练次数"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                      :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="附件">
          <imageUpload v-model="form.appendix"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="openDetail" append-to-body style="width: auto">
      <Detail :reserve-plain-id="reservePlainId"></Detail>
    </el-dialog>
  </div>
</template>

<script>
import {
  createEmergencyReservePlan,
  updateEmergencyReservePlan,
  deleteEmergencyReservePlan,
  getEmergencyReservePlan,
  getEmergencyReservePlanPage,
  exportEmergencyReservePlanExcel
} from "@/api/biz/emergencyReservePlan";
import {getDictDatas, DICT_TYPE} from '@/utils/dict'
import Detail from '../emergencyReservePlainDetail/index.vue'

export default {
  name: "EmergencyReservePlan",
  components: {
    Detail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急预案列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        level: null,
        type: null,
        responsible: null,
        remark: null,
        startYear: null,
        endYear: null,
        januaryNumberOfDrills: null,
        februaryNumberOfDrills: null,
        marchNumberOfDrills: null,
        aprilNumberOfDrills: null,
        mayNumberOfDrills: null,
        juneNumberOfDrills: null,
        julyNumberOfDrills: null,
        augustNumberOfDrills: null,
        septemberNumberOfDrills: null,
        octoberNumberOfDrills: null,
        novemberNumberOfDrills: null,
        decemberNumberOfDrills: null,
        status: null,
        numberOfFinish: null,
        appendix: null,
        tenantId: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{required: true}],
        responsible: [{required: true}]
      },
      typeDictDatas: getDictDatas(DICT_TYPE.BIZ_EMERGENCY_RESERVE_PLAN_TYPE),
      statusDictDatas: getDictDatas(DICT_TYPE.COMMON_STATUS),
      reservePlainId: undefined,
      appendixString: undefined,
      appendixList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getEmergencyReservePlanPage(params).then(response => {
        this.list = response.data.list;
        for (let i = 0; i < this.list.length; i++) {
          let imageList = this.list[i].appendix.split(",")
          if (imageList !== null && imageList.length > 0) {
            this.list[i].appendix = imageList[0]
          }
        }
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        level: undefined,
        type: undefined,
        responsible: undefined,
        remark: undefined,
        startYear: undefined,
        endYear: undefined,
        januaryNumberOfDrills: undefined,
        februaryNumberOfDrills: undefined,
        marchNumberOfDrills: undefined,
        aprilNumberOfDrills: undefined,
        mayNumberOfDrills: undefined,
        juneNumberOfDrills: undefined,
        julyNumberOfDrills: undefined,
        augustNumberOfDrills: undefined,
        septemberNumberOfDrills: undefined,
        octoberNumberOfDrills: undefined,
        novemberNumberOfDrills: undefined,
        decemberNumberOfDrills: undefined,
        status: undefined,
        numberOfFinish: undefined,
        appendix: undefined,
        deptId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应急预案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getEmergencyReservePlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急预案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.appendix != undefined) {
        this.appendixList.length = 0
        for (let i = 0; i < this.form.appendix.length; i++) {
          this.appendixList.push(this.form.appendix[i].url)
        }
        this.form.appendix = _.join(this.appendixList, ",")
      }

      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateEmergencyReservePlan(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createEmergencyReservePlan(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除应急预案编号为"' + id + '"的数据项?').then(function () {
        return deleteEmergencyReservePlan(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有应急预案数据项?').then(() => {
        this.exportLoading = true;
        return exportEmergencyReservePlanExcel(params);
      }).then(response => {
        this.$download.excel(response, '应急预案.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    // 点击详情按钮
    clickDetail(row) {
      let id = row.id
      this.openDetail = true
      this.reservePlainId = row.id
    }
  }
};
</script>
